<template>
  <div class="app-container">
    <div class="flex-between">
      <div class="flex-between">
        <!-- <el-button
          type="primary"
          @click="handleInAddPrescription"
        >新增药方</el-button> -->
      </div>
      <el-input
        v-model="search"
        class="search-main"
        placeholder="请输入用户的名字查询"
        prefix-icon="el-icon-search"
        @keyup.enter.native="HandleSearch"
      />
    </div>
    <el-table
      :data="prescriptionList"
      style="width: 100%;margin-top:30px;"
      border
    >
      <el-table-column
        align="center"
        label="编号"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="类型"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作人员"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.adminName }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="药品"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.medicineName }}
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="数量"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="单价"
        width=""
      >
        <template slot-scope="scope">
          ￥{{ scope.row.price }}
        </template>
      </el-table-column>
          <el-table-column
        align="center"
        label="总价"
        width=""
      >
        <template slot-scope="scope">
          ￥{{ scope.row.price*scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="创建时间"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="备注"
      >
        <template slot-scope="scope">
          {{ scope.row.mark }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page="pageNum"
      :page-size="pageSize"
      layout="total,sizes,  prev, pager, next, jumper"
      :total="totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getMedicineCheckInOutList } from '@/api/medicine'
import { timestampToTime } from '@/utils/time'
export default {
  data() {
    return {
      routes: [],
      prescriptionList: [],
      dialogVisible: false,
      checkStrictly: false,
      prescription: {},
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      pageNum: 1, // 默认每页显示1条数据
      pageSize: 10,
      loading: false,
      totalCount: 0,
      search: ''
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    // this.getRoutes()
    if (this.$store.state.user.roles === 'doctor') {
      this.getPrescription({
        adminId: this.$store.state.user.adminId,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    } else {
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    }
  },
  methods: {
    async getPrescription(data) {
      const res = await getMedicineCheckInOutList(data)
      console.log(res)
      for (const i in res.data.list) {
        res.data.list[i].createTime = timestampToTime(res.data.list[i].createTime)
        res.data.list[i].type = res.data.list[i].type === 1 ? '入库' : '出库'
      }
      this.prescriptionList = res.data.list
      this.totalCount = res.data.total
    },
    generateArr(routes) {
      let data = []
      routes.forEach(route => {
        data.push(route)
        if (route.children) {
          const temp = this.generateArr(route.children)
          if (temp.length > 0) {
            data = [...data, ...temp]
          }
        }
      })
      return data
    },
    handleEdit(scope) {

    },
    handleSizeChange(page) {
      this.pageSize = page
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    handleCurrentChange(page) {
      this.pageNum = page
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    cancel() {
      this.dialogVisible = false
    },
    HandleSearch() {
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        username: this.search
      })
    },
    handleInAddPrescription() {
      this.$router.push('/doctor/prescriptionDetail/:prescriptionId')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
